package cn.fz.handler;

import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.util.SaResult;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    // 其他代码
    @ExceptionHandler(NotRoleException.class)
    public SaResult handlerNotRoleException(NotRoleException e, HttpServletRequest request){
        return SaResult.error("没有角色:"+e.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public SaResult handlerNotPermissionException(NotPermissionException e, HttpServletRequest request){
        log.error("没有权限: {} ",e.getMessage(),e);
        return SaResult.error("没有权限:"+e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public SaResult handlerException(Exception e, HttpServletRequest request){
        return SaResult.error("系统异常:"+e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public SaResult handlerRuntimeException(RuntimeException e, HttpServletRequest request){
        return SaResult.error("运行时系统异常:"+e.getMessage());
    }
}
